<template>
  <div>
    <a-card class="">
      <a-tabs v-model:active-key="formState.applyPath" @change="getData">
        <a-tab-pane :key="0" tab="门诊" />
        <a-tab-pane :key="1" tab="住院" />
        <a-tab-pane :key="2" tab="体检" />
      </a-tabs>
      <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
        <a-form ref="formRef" class="form-style-flex" :model="formState" :colon="false" :label-col="{ style: { width: '80px' } }">
          <a-form-item name="pyjm">
            <a-input
              v-model:value="formState.pyjm"
              class="w-80"
              :placeholder="formState.applyPath === 1 ? '姓名/助记码/手机号/床位号' : '姓名/助记码/手机号'"
            />
          </a-form-item>
          <a-form-item name="stdate" label="申请日期">
            <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
          </a-form-item>
          <a-form-item name="printStatus" label=" " :label-col="{ style: { width: '80px' } }">
            <a-radio-group v-model:value="formState.printStatus">
              <a-radio value="">全部</a-radio>
              <a-radio :value="0">未打印</a-radio>
              <a-radio :value="1">已打印</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-form>
      </SearchContainer>
    </a-card>
    <a-card class="mt-2">
      <div class="absolute top-[-9999px] hidden">
        <a-button ref="printMe" v-print="{ id: 'printMe' }"> 打印条形码 </a-button>
        <PrintView id="printMe" :print-data="printData" />
      </div>
      <BaseTable
        :data-source="tableData"
        fill
        :loading="loading"
        :pagination="pagination"
        :column-code="formState.applyPath === 0 ? '000482' : '000735'"
        :scroll="{ x: 1800 }"
        row-key="lsh"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'printStatus'">
            <a-tag v-if="!record.barCode" color="blue">未打印</a-tag>
            <a-tag v-else color="green">已打印</a-tag>
          </template>
          <template v-if="column.dataIndex === 'status'">
            <a-tag v-if="record.status === '0'" color="blue">未审核</a-tag>
            <a-tag v-else-if="record.status === '1'" color="green">已审核</a-tag>
            <a-tag v-else-if="record.status === '3'" color="orange">已签署</a-tag>
            <a-tag v-else color="red">已作废</a-tag>
          </template>
          <template v-if="column.dataIndex === 'categroyName'">
            <PopoverTable :config="detailConfig" :params="{ categroyCode: record.categroyCode, lsh: record.lsh }" :width="600">
              {{ record.categroyName }}
            </PopoverTable>
          </template>
          <!-- <template v-if="column.dataIndex === 'barCode'">
            <PopoverTable :config="detailConfig" :params="{ categroyCode: record.categroyCode, lsh: record.lsh }" :width="600">
              {{ record.barCode }}
            </PopoverTable>
          </template>  || record.jfzt === 0 -->
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" :disabled="record.status === '2'" @click="record.barCode ? onPrintCode(record) : onBarCode(record)">
              打印
            </a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
// import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
import { getSystemConfig } from '@/utils/systemConfig'
import PrintView from './PrintView.vue'

const formRef = ref(null)
const printData = ref({})
const military = ref()
const printMe = ref()
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  applyPath: 0,
  printStatus: ''
})
const detailConfig = ref({
  url: '/lis/LisApply/getlisInstrumentTypeRelationByCategroyCodeList',
  columnCode: '000183',
  rowKey: 'brId',
  formatResult: (data) => {
    const list = data.filter((item) => {
      return item.chek === 1
    })
    return list.map((item, index) => {
      return {
        NO: index + 1,
        ...item
      }
    })
  }
})

const { loading, pagination, getData, tableData } = useTable({
  url: '/lis/LisApply/getLisApplyList',
  immediate: true,
  formatParams: () => {
    return {
      ...formState.value,
      stdate: formState.value.dateRange?.[0],
      eddate: formState.value.dateRange?.[1],
      pyjm: formState.value?.pyjm,
      isDisplayed: 0
    }
  }
})

// 直接打印
const onPrintCode = async (row) => {
  const { code, msg, data } = await post('/lis/lisPrint/printTheInspectionBarcode', { lsh: row.lsh })
  if (code !== 0) return message.error(msg)
  if (military.value) {
    printData.value = JSON.parse(data)
    setTimeout(() => {
      printMe.value.$el.click()
    }, 1000)
  } else {
    window.openExe?.rpt(data) //调用winform打印
  }
  getData()
}

// 获取条码
const onBarCode = async (row) => {
  const { code, msg } = await post('/lis/LisApply/doPrintBarCode', { lsh: row.lsh })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  onPrintCode(row)
}

onMounted(() => {
  military.value = getSystemConfig('NAVYHIS_MODE') === '1' // 1 军用，0非军用
})
</script>
